package jrdcom.com.onecodesample.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import jrdcom.com.onecodesample.Model.WeatherInfoModel;
import jrdcom.com.onecodesample.R;
import jrdcom.com.onecodesample.Util.HandleUtil;
import jrdcom.com.onecodesample.Util.HttpUtil;
import jrdcom.com.onecodesample.View.CustomTitleBar;

/**
 * Created by longcheng on 17/1/23.
 */
public class WeatherInfoActivity extends AppCompatActivity implements CustomTitleBar.TitleBarClickListeter{
    private String country_code;
    private CustomTitleBar customTitleBar;
    private TextView weaUpdateTime;
    private TextView weaInfo;
    private TextView weaTemp;
    private int weatherCode;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_weather_info);
        String city_name = getIntent().getStringExtra("country_name");
        country_code = getIntent().getStringExtra("country_code");
        customTitleBar = (CustomTitleBar)findViewById(R.id.weatherTitle);
        customTitleBar.setBtnClickListeter(this);
        /*init customTitleBar*/
        customTitleBar.setLeftBtnHide(true);
        customTitleBar.setRightBtnHide(true);
        customTitleBar.setTitle(city_name);

        //get views
        weaUpdateTime = (TextView)findViewById(R.id.updateDate);
        weaInfo = (TextView)findViewById(R.id.weatherInfo);
        weaTemp = (TextView)findViewById(R.id.weatherTmp);

        //show local firset
        showWeatherLocal();
        //init
        WeatherInfoModel weatherInfoModel = HandleUtil.getLocalInfo(WeatherInfoActivity.this);
        if (0 != weatherInfoModel.getWeatherCode()) {
            weatherCode = weatherInfoModel.getWeatherCode();
            getWeatherInfoFromSever(1);
        }else{
            getWeatherInfoFromSever(0);
        }
    }

    //get data from server
    private void getWeatherInfoFromSever(final int getType){
        String address =null;
        if(0 == getType){
            address = "http://www.weather.com.cn/data/list3/city" + country_code + ".xml";
        }else if(1 ==getType){
            address = " http://www.weather.com.cn/data/cityinfo/" + weatherCode + ".html";
        }else{
            //error
        }
        showProgressDialog();
        HttpUtil.sendHttpRequest(address, new HttpUtil.HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                Boolean result = false;
                int weather = 0;
                closeProgressDialog();
                //handle respong
                if(0 == getType){
                    weatherCode =  HandleUtil.handleWeatherCodeRespone(response);
                    if(0 != weatherCode){
                        //go to get weather info
                        getWeatherInfoFromSever(1);
                    }else{
                        showWeatherLocal();
                    }
                }else if(1 == getType){
                    result = HandleUtil.handleWeatherInfoRespone(WeatherInfoActivity.this,response, weatherCode);
                    if(true == result){
                        showWeatherLocal();
                    }
                }else{
                    //error info
                    showWeatherLocal();
                }
            }

            @Override
            public void onError(Exception e) {
                closeProgressDialog();
                showWeatherLocal();
            }
        });
    }

    private  void showWeatherLocal(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WeatherInfoModel weatherInfoModel = HandleUtil.getLocalInfo(WeatherInfoActivity.this);
                if (0 != weatherInfoModel.getWeatherCode()) {
                    weaUpdateTime.setText(weatherInfoModel.getWeaUpdateTime());
                    weaInfo.setText(weatherInfoModel.getWeaDes());
                    weaTemp.setText(weatherInfoModel.getWeaTemp1() + "~" + weatherInfoModel.getWeaTemp2());
                } else {
                    weaUpdateTime.setText("????-??-??");
                    weaInfo.setText("--------");
                    weaTemp.setText("?~?");
                }
            }
        });

    }

    @Override
    public void LeftBtnClick() {
        finish();
    }

    @Override
    public void RightBtnClick() {
        getWeatherInfoFromSever(0);
    }

    @Override
    public void TitleClick() {
        Intent intent = new Intent( this, MainPageActivity.class);
        startActivity(intent);
    }

    private void showProgressDialog(){
        if(null == progressDialog){
            progressDialog =new ProgressDialog(this);
            progressDialog.setMessage("Loading");
            progressDialog.setCanceledOnTouchOutside(true);
        }

    }
    private void closeProgressDialog(){
        if( progressDialog!= null){
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

}
